package com.zoiz.plus;

import com.zoiz.plus.renderer.Renderer;

/**
 * Used for holds an interceptor chain.
 * 
 * @author Michael Liao (askxuefeng@gmail.com)
 */
class InterceptorChainImpl implements InterceptorChain {

	private final Interceptor[] interceptors;
	private int index = 0;
	private Renderer result;

	public Renderer getResult() {
		return result;
	}

	InterceptorChainImpl(Interceptor[] interceptors) {
		this.interceptors = interceptors;
	}

	public void doInterceptor(Execution execution) throws Exception {
		if (index == interceptors.length)
			result =  execution.execute();
		else {
			// must update index first, otherwise will cause stack overflow:
			index++;
			interceptors[index - 1].intercept(execution, this);
		}
	}
}
